package homework;

class Node{
    private int val;
    private Node next;

    public Node(int val) {
        this.val = val;
    }
    public void delete(){

    }

    public int getVal() {
        return val;
    }

    public void setVal(int val) {
        this.val = val;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }
    //增加节点
    public Node add(Node head,int val){
        Node cur=head;
        while (cur.next!=null){
            cur=cur.next;
        }
        cur.next=new Node(val);
        return head;
    }
    //删除节点
    public boolean delete(Node head,int val){
        if(!contain(head,val)){
            return false;
        }
        if(head.val==val){
            head=head.next;
            return true;
        }
        Node cur1=head;
        Node cur2=head.next;
        while (cur2!=null){
            if(cur2.next.val==val){
                cur1.next=cur2.next.next;
                return true;
            }
            cur1=cur1.next;
            cur2=cur2.next;
        }
        return false;
    }
    //链表长度
    public int size(Node head){
        Node cur=head;
        int count=0;
        while (cur!=null){
            cur=cur.next;
            count++;
        }
        return count;
    }
    //看链表中是否有这个节点
    public boolean contain(Node head,int val){
        Node cur=head;
        while (cur!=null){
            if(cur.getVal()==val){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }
}
public class HomeWork15 {

}
